package com.zcs.app.lucky.adapter

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.sxw.android.base.adapter.CommonRecyclerAdapter
import cn.sxw.android.base.utils.GlideStaticUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zcs.app.lucky.R
import com.zcs.app.lucky.entity.LuckyPeople
import com.zcs.app.lucky.entity.PrizeEntity
import java.util.*

class AdapterHelper {
    // 奖品列表
    var prizeAdapter: CommonRecyclerAdapter<PrizeEntity>? = null
    val prizeList = ArrayList<PrizeEntity>()

    fun initPrizeAdapter(
        recyclerView: RecyclerView,
        checkable: Boolean = false,
        horizontal: Boolean = false,
        spanCount: Int = 6,
        onClick: (item: PrizeEntity) -> Unit
    ) {
        if (prizeAdapter == null) {
            val layoutId = if (horizontal)
                R.layout.item_prize_row
            else
                R.layout.item_prize_column
            prizeAdapter =
                object : CommonRecyclerAdapter<PrizeEntity>(layoutId, prizeList) {
                    override fun convert(holder: BaseViewHolder, item: PrizeEntity) {
                        holder.setText(R.id.tvName, item.name)
                        holder.setText(R.id.tvLevel, item.level!!.name)
                        holder.setText(R.id.tvMaxCount, "名额：${item.maxCount}")
                        if (checkable) {
                            val bg = holder.getView<View>(R.id.bg)
                            bg.isSelected = item.isChecked
                        }
                        GlideStaticUtils.displayImage(
                            item.pic,
                            R.drawable.empty_photo,
                            holder.getView(R.id.ivThumb)
                        )
                    }
                }
            recyclerView.adapter = prizeAdapter
            prizeAdapter!!.setOnItemClickListener { adapter, _, position ->
                val clickItem = prizeList[position]
                if (checkable) {
                    for (p in prizeList)
                        p.isChecked = false
                    clickItem.isChecked = !clickItem.isChecked
                    adapter.notifyDataSetChanged()
                }
                onClick(clickItem)
            }
        } else {
            prizeAdapter!!.notifyDataSetChanged()
        }
        if (recyclerView.layoutManager != null && recyclerView.layoutManager is GridLayoutManager) {
            val layoutManager = recyclerView.layoutManager as GridLayoutManager
            layoutManager.spanCount = spanCount
        }
    }


    // 用户列表
    private var userAdapter: CommonRecyclerAdapter<LuckyPeople>? = null
    val userList = ArrayList<LuckyPeople>()

    fun initUserAdapter(
        recyclerView: RecyclerView,
        spanCount: Int = 6,
        onClick: (id: String) -> Unit
    ) {
        if (userAdapter == null) {
            userAdapter =
                object : CommonRecyclerAdapter<LuckyPeople>(R.layout.item_lucky_people, userList) {
                    override fun convert(holder: BaseViewHolder, item: LuckyPeople) {
                        val ivAvatar = holder.getView<ImageView>(R.id.ivAvatar)
                        holder.setVisible(R.id.ivDelete, false)
                        if (item.isEmpty()) {
                            ivAvatar.visibility = View.INVISIBLE
                            holder.setText(R.id.tvName, "")
                        } else {
                            ivAvatar.visibility = View.VISIBLE
                            GlideStaticUtils.displayCircleImage(
                                item.avatar,
                                R.mipmap.ic_launcher_round,
                                ivAvatar
                            )
                            holder.setText(R.id.tvName, item.name)
                        }
                    }
                }
            recyclerView.adapter = userAdapter
        } else {
            userAdapter!!.notifyDataSetChanged()
        }

        if (recyclerView.layoutManager != null && recyclerView.layoutManager is GridLayoutManager) {
            val layoutManager = recyclerView.layoutManager as GridLayoutManager
            layoutManager.spanCount = spanCount
        }
    }
}